﻿//
// Copyright (c) 2010
// by Ashish Agrawal and Chris Paterra
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using DotNetNuke.Web.Mvp;
using SimpleBlog.Components.Data;
using SimpleBlog.Components.Controllers;
using SimpleBlog.Components.Models;
using SimpleBlog.Components.Views;
using SimpleBlog.Components.Entities;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using System;

namespace SimpleBlog.Components.Presenters 
{

	/// <summary>
	/// 
	/// </summary>
	public class EntryPresenter : ModulePresenter<IEntryView, EntryModel>
	{

		#region Private Members

		protected ISimpleBlogController Controller { get; private set; }

		/// <summary>
		/// 
		/// </summary>
		private int EntryID {
			get {
				var itemId = Null.NullInteger;
				if (!String.IsNullOrEmpty(Request.Params["id"])) itemId = Int32.Parse(Request.Params["id"]);
				return itemId;
			}
		}

		#endregion

		#region Constructor

		/// <summary>
		/// 
		/// </summary>
		/// <param name="view"></param>
		public EntryPresenter(IEntryView view) : this(view, new SimpleBlogController(new SqlDataProvider()))
		{
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="view"></param>
		/// <param name="controller"></param>
		public EntryPresenter(IEntryView view, ISimpleBlogController controller) : base(view)
		{
			if (view == null) {
				throw new ArgumentException(@"View is nothing.", "view");
			}

			if (controller == null) {
				throw new ArgumentException(@"Controller is nothing.", "controller");
			}

			Controller = controller;
		}

		#endregion

		#region Events

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void Cancel(object sender, EventArgs e) {
			Response.Redirect(ModuleContext.NavigateUrl(ModuleContext.TabId, "", false, ""));
		}

		/// <summary>
		/// 
		/// </summary>
		protected override void OnLoad() {
			base.OnLoad();

			if (EntryID != Null.NullInteger) 
			{
				var objBlog = Controller.GetEntry(EntryID, ModuleContext.ModuleId);
				if (objBlog != null) 
				{
					View.Model.BlogEntry = objBlog;
				} 
				else
					Response.Redirect(ModuleContext.NavigateUrl(ModuleContext.TabId, "", false, ""));
			} 
			else 
			{
				View.Model.BlogEntry = new EntryInfo();
			}

			View.Cancel += Cancel;
			View.Model.IsEditable = ModuleContext.IsEditable;
			View.Model.CurrentUserID = ModuleContext.PortalSettings.UserId;
			View.Refresh();
		}

		#endregion

	}
}